<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

error_reporting(E_ERROR | E_PARSE );

/**
 *  系统日志函数，用于记录日志信息
 * @param string $explain
 * @param string $message
 * @param null $path
 */
function sysLogs($explain='未知' ,$message='' ,$path=null)
{
    $syslogs = model("Syslog");
    $data = array();
    if (empty($path)){
        $data['module'] = request()->module();
        $data['controller'] = request()->controller();
        $data['action'] = request()->action();
    } else {
        $arr = explode('/', $path);
        switch (count($arr)){
            case 1:
                $data['module'] = $arr[0];
                break;
            case 2:
                $data['module'] = $arr[0];
                $data['controller'] = $arr[1];
                break;
            case 3:
                $data['module'] = $arr[0];
                $data['controller'] = $arr[1];
                $data['action'] = $arr[2];
                break;
        }
    }
    $data['explain'] = $explain;
    $data['message'] = $message;
    $syslogs->create($data);
}

function showFileUrl($file_id) {
    if (empty($file_id)){
        return '';
    }
    $file = model('UploadFile')->where('id', $file_id)->value('file');
    if (empty($file)){
        return null;
    }
    return $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$file;
}
function p($data){
    dump($data);
    exit;
}

/**
 * 获取订单号
 * @return string
 */
function getOrderNumber(){
    list($decimal, $timestamp) = explode(' ', microtime());
    $time = date('YmdHis',$timestamp);
    $micro = sprintf('%08d', (int)($decimal*100000000));
    $rand = sprintf('%06d', mt_rand(1, 999999));
    return $time.$micro.$rand;
}
/**
 * 序列化
 * @param $value
 * @param string $type
 * @return false|string|null
 */
function serializeByArray($value,$type='json'){
    if (empty($value)){
        return null;
    }
    if ($type == 'json'){
        return json_encode($value,JSON_UNESCAPED_UNICODE);
    }
    if (is_array($value)){
        return implode($type,$value);
    } else {
        return $value;
    }
}

/**
 * 反序列化
 * @param $value
 * @param string $type
 * @return false|mixed|string|null
 */
function unSerializeByJson($value, $type='json'){
    if (empty($value)){
        return null;
    }
    if ($type == 'json'){
        return json_decode($value, true);
    }
    return explode($type, $value);
}

/**
 * 日期函數
 * @param $value
 * @param string $format
 * @return false|string
 */
function showDate($value, $format='Y-m-d'){
    return date($format, $value);
}

/**
 * 時間函數
 * @param $value
 * @param string $format
 * @return false|string
 */
function showTime($value, $format='Y-m-d H:i:s'){
    return date($format, $value);
}

/**
 * 獲取当前用戶全部信息
 * @return mixed|null
 */
function getUserInfo(){
    $login_key = \app\admin\controller\Base::$_login_key;
    return  \app\extra\ApiToken::get($login_key);
}
/**
 * 获取当前用户id
 * @return mixed|null
 */
function getUserID(){
    $user = getUserInfo();
    return empty($user)? null : $user['id'];
}

/**
 * 获取api模块用户登陆的信息
 * @return mixed|null
 */
function getPersonInfo(){
    $login_key = \app\api\controller\Base::$_login_key;
    return  \app\extra\ApiToken::get($login_key);
}

/**
 * 获取当前用户id
 * @return mixed|null
 */
function getPersonId(){
    $user = getPersonInfo();
    return empty($user)? null : $user['id'];
}

/**
 * 获取当前用户姓名
 * @return mixed
 */
function getUserName(){
    $user = getUserInfo();
    return $user['nickname']? $user['nickname']:$user['username'];
}

/**
 * 设置域名头
 * @param $str
 * @return string|null
 */
function added_domain($str){
    if (empty($str)){
        return null;
    }
    $preg = "/^http(s)?:\\/\\/.+/";
    if(preg_match($preg, $str)) {
        return $str;
    } else {
        return request()->domain().$str;
    }
}

/**
 * 通过数组给链接加上域名
 * @param $value
 * @return array|string|null
 */
function list_added_domain($value){
    if (empty($value)){
        return [];
    }
    if (is_string($value)){
        return added_domain($value);
    }
    foreach ($value as &$item){
        $item = added_domain($item);
    }
    return $value;
}

function is_we_chat()
{
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    }
    return false;
}

function fullPath($path, $option=''){
    return Url($path, $option, true, true);
}

/**
 * 取随机值
 * @param int $length
 * @param int $type 数字：1，小写：2，大写：4，合并请用“|”
 * @return string
 */
function genRandomCode($length = 8, $type=7){
    $arr = [];
    if ($type & 1){
        $arr =      [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,0];
    }
    if ($type & 2){
        $letter =   ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
        $arr = array_merge($arr, $letter);
    }
    if ($type & 4){
        $cap =      ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        $arr = array_merge($arr, $cap);
    }
    $size = count($arr)-1;
    $code = '';
    for ($i=0;$i < $length; $i++){
        $sub = mt_rand(0,$size);
        $code .= $arr[$sub];
    }
    return $code;
}